import Vue from 'vue'
import VueRouter from 'vue-router'
import User from "@/views/user/User.vue";
import {f} from "core-js/internals/new-promise-capability";
import Cookies from "js-cookie";

Vue.use(VueRouter)

const routes = [
  //   admin

  {
    path: '/',
    name: 'home',
    component: () =>import('../views/Layout.vue'),
    // 子路由
    children:[
      {
        path: 'homev',
        name: 'homev',
        component:() =>import('../views/home/HomeView.vue')
      },
      {

        path: 'adminlist',
        name: 'adminlist',
        component: () =>import('../views/admin/User.vue')
      },
      {
        path: 'AddAdmin',
        name: 'AddAdmin',
        component: () =>import('../views/admin/Add.vue')
      },
      {
        path: 'EditAdmin',
        name: 'EditAdmin',
        component: () =>import('../views/admin/Edit.vue')
      },

      // user
      {
        path: 'userlist',
        name: 'userList',
        component: () =>import('../views/user/User.vue')
      },
      {
        path: 'AddUser',
        name: 'AddUser',
        component: () =>import('../views/user/AddUser.vue')
      },
      {
        path: 'EditUser',
        name: 'EditUser',
        component: () =>import('../views/user/EditUser.vue')
      },
      {
        path: 'BookList',
        name: 'BookList',
        component:() =>import('../views/categrary/BookList.vue')
      },
      {
        path: 'AddBook',
        name: 'AddBook',
        component:() =>import('../views/categrary/AddBook.vue')
      },
      {
        path: 'EditBook',
        name: 'EditBook',
        component:() =>import('../views/categrary/EditBook.vue')
      }
    ]

  },
  {
    path: '/Login',
    name: 'Login',
    component: ()=>import('../views/login/Login.vue')
  },

  {
    path: '/about',
    name: 'about',
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: "*",
    component:()=>import('../views/404.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next) =>{
  if(to.path==='/Login') next()
  const admin =Cookies.get("user")
  if(!admin && to.path!=="/Login") return next("/Login")
  next()
})
export default router
